# 简单测试文件类型API端点

# 配置
$baseUrl = "http://localhost:5269/api/file/types"

# 生成随机ID以避免类型名称冲突
$randomId = Get-Random -Minimum 1000 -Maximum 9999
$testTypeName = "测试类型$randomId"

# 测试1: 获取所有文件类型（GET）
Write-Host "\n=== 测试1: 获取所有文件类型（GET）==="
try {
    $response = Invoke-WebRequest -Uri $baseUrl -Method Get -UseBasicParsing
    Write-Host "✅ 成功! 状态码: $($response.StatusCode)"
    Write-Host "文件类型数量: $(($response.Content | ConvertFrom-Json).Count)"
} catch {
    Write-Host "❌ 失败! 错误: $($_.Exception.Message)"
}

# 测试2: 添加新文件类型（POST）
Write-Host "\n=== 测试2: 添加新文件类型（POST）==="
Write-Host "尝试添加: $testTypeName"

$requestBody = @{
    TypeName = $testTypeName
    MemoryStrength = 1.2
}

$jsonBody = ConvertTo-Json -InputObject $requestBody

try {
    $response = Invoke-WebRequest -Uri $baseUrl -Method Post -Body $jsonBody -ContentType "application/json" -UseBasicParsing
    Write-Host "✅ 成功! 状态码: $($response.StatusCode)"
    Write-Host "添加结果: $($response.Content)"
} catch {
    Write-Host "❌ 失败! 错误: $($_.Exception.Message)"
    if ($_.Exception.Response) {
        $statusCode = $_.Exception.Response.StatusCode.value__
        Write-Host "状态码: $statusCode"
        if ($_.ErrorDetails) {
            Write-Host "错误内容: $($_.ErrorDetails.Message)"
        }
    }
}

# 测试3: 验证新文件类型是否存在
Write-Host "\n=== 测试3: 验证新文件类型是否存在 ==="
try {
    $response = Invoke-WebRequest -Uri $baseUrl -Method Get -UseBasicParsing
    $allTypes = $response.Content | ConvertFrom-Json
    $addedType = $allTypes | Where-Object { $_.TypeName -eq $testTypeName }
    
    if ($addedType) {
        Write-Host "✅ 成功! 新文件类型 '$testTypeName' 已添加到系统中!" -ForegroundColor Green
        Write-Host "类型详情: $(ConvertTo-Json $addedType)"
    } else {
        Write-Host "❌ 失败! 未找到新文件类型 '$testTypeName'" -ForegroundColor Red
    }
} catch {
    Write-Host "❌ 失败! 错误: $($_.Exception.Message)"
}

Write-Host "\n=== 测试完成 ==="